import json
import os
from django.http import HttpResponse, JsonResponse
from django.utils import timezone
from History.models import History
from Message.models import Message
def add(request):
    if request.method == 'POST':
        body = json.loads(request.body)
        History.objects.create(
            content_type=body["content_type"],
            type=body["type"],
            senderAccount=body["senderAccount"],
            accepterAccount=body["accepterAccount"],
            content=body['content']
        ).save()
        if body["type"]=="用户":
            if body["content_type"]=="文件":
                Message.objects.create(
                    type="消息通知",
                    senderAccount=body["senderAccount"],
                    accepterAccount=body["accepterAccount"],
                    content="文件"
                ).save()
            if body["content_type"]=="文本":
                Message.objects.create(
                    type="消息通知",
                    senderAccount=body["senderAccount"],
                    accepterAccount=body["accepterAccount"],
                    content="您收到了一条好友聊天"
                ).save()
        if body["type"] == "群组":
            if body["content_type"]=="文件":
                Message.objects.create(
                    type="消息通知",
                    senderAccount=body["senderAccount"],
                    accepterAccount="group"+body["accepterAccount"],#groupId
                    content="文件"
                ).save()
            if body["content_type"]=="文本":
                Message.objects.create(
                    type="消息通知",
                    senderAccount=body["senderAccount"],
                    accepterAccount="group"+body["accepterAccount"],#groupId
                    content="您收到了一条群消息"
                ).save()
        return HttpResponse("添加成功")
    else:
        return HttpResponse("请使用POST")
def upload(request):
    file = request.FILES.get('file', None)
    if not file:
        return HttpResponse("no file ")
    filename = str(timezone.now()) + "_" + file.name
    filename = filename.replace(" ", "")
    filename = filename.replace(":", "")
    filename = filename.replace("-", "")
    filename = filename.replace("+", "")
    filename = filename.replace("_", "")
    destination = open(os.path.join("files/history", filename), "wb+")
    for chunk in file.chunks():
        destination.write(chunk)
    destination.close()
    return HttpResponse("files/history/" + filename)
def delete(request):
    if request.method == 'GET':
        History.objects.filter(id=request.GET.get("id")).delete()
        return HttpResponse("删除成功")
    else:
        return HttpResponse("请使用GET")
def deleteAll(request):
    if request.method == 'GET':
        h1=History.objects.filter(senderAccount=request.GET.get("senderAccount"),accepterAccount=request.GET.get("accepterAccount"))
        h2=History.objects.filter(accepterAccount=request.GET.get("senderAccount"),senderAccount=request.GET.get("accepterAccount"))
        for each in h1:
            if each.content_type=="文件":
                os.remove(each.content)
        for each in h2:
            if each.content_type=="文件":
                os.remove(each.content)
        Message.objects.filter(accepterAccount=request.GET.get("accepterAccount"),senderAccount=request.GET.get("senderAccount"),type="消息通知").delete()
        Message.objects.filter(accepterAccount=request.GET.get("senderAccount"),senderAccount=request.GET.get("accepterAccount"),type="消息通知").delete()
        h1.delete()
        h2.delete()
        return HttpResponse("删除成功")
    else:
        return HttpResponse("请使用GET")
def query(request):
    if request.method == 'GET':
        if request.GET.get("type")=="user":
            h = History.objects.filter(accepterAccount=request.GET.get("accepterAccount"),senderAccount=request.GET.get("senderAccount")) | History.objects.filter(senderAccount=request.GET.get("accepterAccount"), accepterAccount=request.GET.get("senderAccount"))
            return JsonResponse({"data": [{
                'type': each.type,
                'content_type': each.content_type,
                'accepterAccount': each.accepterAccount,
                'senderAccount': each.senderAccount,
                'content': each.content,
                'createTime': each.createTime,
                'id': each.id,
            }for each in h]
            })
        if request.GET.get("type")=="group":
            h = History.objects.filter(accepterAccount=request.GET.get("accepterAccount"))
            return JsonResponse({"data": [{
                'type': each.type,
                'content_type': each.content_type,
                'accepterAccount': each.accepterAccount,
                'senderAccount': each.senderAccount,
                'content': each.content,
                'createTime': each.createTime,
                'id': each.id,
            } for each in h]
            })
    else:
        return HttpResponse("请使用GET")
def sendGroupInvitation(request):
    body = json.loads(request.body)
    for each in eval(str(body["friendAccountList"])):
        History.objects.create(
            content_type="文本",
            type="用户",
            senderAccount=body["account"],
            accepterAccount=each,
            content="我邀请了你加入群聊"+body["groupId"]+"，点击查看"
        ).save()
    return HttpResponse("")
